//index.js
import config from '../../config.js';
const base = config;

//获取应用实例
const app = getApp();
Page({
  data: {
    // 功能列表
    funList: [{
      icon: "https://app.longchg.com/gudongcar/images/chexian.png",
      text: "车险报价",
      url: "../insurance/insurance"
    }, {
      icon: "https://app.longchg.com/gudongcar/images/shiguxiezhu.png",
      text: "事故协助",
      url: "../accidentAssi/accidentAssi"
    }, {
      icon: "https://app.longchg.com/gudongcar/images/weizhangbanli.png",
      text: "违章办理",
      url: "../peccancy/step2"
    }, {
      icon: "https://app.longchg.com/gudongcar/images/daiweixiu.png",
      text: "代维修",
      url: "../repair/repair"
    }, {
      icon: "https://app.longchg.com/gudongcar/images/daibaoyang.png",
      text: "代保养",
      url: "../maintenance/maintenance"
    }, {
      icon: "https://app.longchg.com/gudongcar/images/member.png",
      text: "菇咚会员",
      url: "../member/activation"
    },
    {
      icon: "https://app.longchg.com/gudongcar/images/jiesongji.png",
      text: "接送机",
      url: "../airport/airport"
    }, {
      icon: "https://app.longchg.com/gudongcar/images/daibuche.png",
      text: "代步车",
      url: "../stepcar/stepcar"
    },
    {
      icon: "https://app.longchg.com/gudongcar/images/dainianjian.png",
      text: "代年检",
      url: "../yearaudit/yearaudit"
    }, {
      icon: "https://app.longchg.com/gudongcar/images/jiuyuan.png",
      text: "菇咚金融",
      url: "../finance/loanPlan"
      //url: "../rescure/rescure"
    },
      /** {
         icon: "https://app.longchg.com/gudongcar/images/gengduo.png",
         text: "其他",
         url: "../others/others"
       }**/
    ]
  },

  //删除车辆
  deleteCar: function (e) {
    let id = e.currentTarget.dataset.id,
      index = e.currentTarget.dataset.index,
      that = this;
    wx.showLoading({
      title: '正在删除',
    })
    wx.request({
      url: base.carDelete,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        id: id
      },
      success: function (res) {
        wx.hideLoading();
        console.log(res);
        if (res.data.status == 200) {
          wx.showToast({
            title: '删除成功',
          })
          let arr = that.data.carList;
          arr.splice(index, 1);
          that.setData({
            carList: arr
          })
        } else {
          wx.showToast({
            title: "删除失败",
            icon: "none"
          })
        }
      }
    })
  },



  //违章查询
  illegalQuery: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../peccancyList/peccancyList?id=' + id,
    })
  },

  onLoad: function () {
    // var userType = wx.getStorageSync('userType');
    // console.log(userType);
    // if (userType == 1) {
    //   wx.reLaunch({
    //     url: '../gjIndex/gjindex/gjindex',
    //   })
    // }



    //请求管家列表
    var that = this;
    wx.request({
      url: base.mainInfo,
      header: {
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        id: 1
      },
      method: "POST",
      success: function (res) {
        console.log("首页信息");
        console.log(res.data.data);
        that.setData({
          mainInfo: res.data.data,
          funList: res.data.data.menus
        })

        wx.setStorageSync("services", that.data.mainInfo.services);
      }
    })

    //请求车辆列表
    wx.request({
      url: base.myCarsList,
      header: {
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        start: 0
      },
      success: function (res) {
        console.log(res);
        if (res.statusCode == 200) {
          that.setData({
            carList: res.data.data
          })
        } else {
          wx.showToast({
            title: '车辆列表获取失败',
            icon: 'none'
          })
        }
      }
    })


  },
  tipBannerDetail: function (e) {
    var url = e.currentTarget.dataset.url;
    console.log("banner");
    wx.navigateTo({
      url: url,
    })
  },
  tipDetail: function (e) {
    var url = e.currentTarget.dataset.url;
    console.log("tip");
    wx.navigateTo({
      url: '../newsDetail/newsDetail',
    })
  },
  onShow: function () {
    var houseKeeper = wx.getStorageSync("MyDefaultHouseKeeper");
    if (houseKeeper) {
      this.setData({
        "mainInfo.keeper": houseKeeper
      })

      console.log(this.data.mainInfo);
    }
  }
})